﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StoryTeller.Domain;
using StoryTeller.Domain.Exceptions;
using StoryTellerSite.Models;
using StoryTeller.Repository;
using System.Text;

namespace StoryTellerSite.Controllers
{
    public class StoryController : BasePagerController<Story>
    {
        //
        // GET: /Story/

        #region story
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Story story)
        {
            try
            {
                StoryTeller.Repository.StoryRepository rep = new StoryTeller.Repository.StoryRepository();
                story.Group = new Group()
                {
                    Title = story.Title,
                    Owner = CurrentUser,
                    IsPublic = story.IsPublic,
                    CreationDate = DateTime.Now,
                    LastUpdate = DateTime.Now
                };

                rep.Insert(story);
                return new JsonRedirect("Contribute", "Create", this.ControllerContext);
            }
            catch (ValidationMessagesException vex)
            {
                return new JsonError("pvMessages", new MessageModel(vex), this.ControllerContext);
            }
            catch (FriendlyErrorMessageException fex)
            {
                return new JsonError("pvMessages", new MessageModel(fex), this.ControllerContext);
            }
        }
        #endregion

        #region Contribution

        [HttpGet]
        public ActionResult Contribute(int story)
        {

            Story s = new StoryRepository().Select(story);
            s.Chapters = new ChapterRepository().SelectByStoryId(story);
            return View(s);
        }


        [HttpPost]
        public ActionResult Contribute(Contribution contribution)
        {
            try
            {
                ContributionRepository r = new ContributionRepository();
                contribution.CreationDate = DateTime.Now;
                contribution.LastUpdate = DateTime.Now;
                contribution.Owner = CurrentUser;
                contribution.Chapter = new ChapterRepository().Select(contribution.Chapter.Id);
                r.Insert(contribution);
                return new JsonPartial(contribution, "pv_contribution", "#StoryViewer", true, this.ControllerContext);
            }
            catch (ValidationMessagesException vex)
            {
                return new JsonError("pvMessages", new MessageModel(vex), this.ControllerContext);
            }
            catch (FriendlyErrorMessageException fex)
            {
                return new JsonError("pvMessages", new MessageModel(fex), this.ControllerContext);
            }
        }

        #endregion



        #region paginação

        public override string PartialViewName
        {
            get { return "pv_StoryList"; }
        }

        public override string Controller
        {
            get { return "Story"; }
        }

        public override string JQuerySelector
        {
            get { return "#DivList"; }
        }

        public override bool Append
        {
            get { return true; }
        }

        public override StoryTeller.Domain.Repository.IPagerRepository<Story> Repository
        {
            get { return new StoryRepository(); }
        }

        public override bool UsePager
        {
            get { return false; }
        }

        public override int CountPerPage
        {
            get
            {
                return 2;
            }
        }

        #endregion



    }
}
